package edu.uoc.tdp.pac3.client;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.awt.Font;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import edu.uoc.tdp.pac3.common.GestorIncidenciesInterface;
import edu.uoc.tdp.pac3.common.tools.TDSLanguageUtils;

/**
 * Classe Client 
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class Client extends JFrame {

    private JPanel jContentPane = null;
    private JMenuBar mnuMain = null;
    private JMenu mnuInici = null;
    private JMenuItem mnuIniciIt1 = null;
    private JMenuItem mnuIniciIt2 = null;
    private JMenu mnuIncidencies = null;
    private JMenuItem mnuIncidenciesIt1 = null;
    private JMenuItem mnuIncidenciesIt2 = null;
    private JMenuItem mnuIncidenciesIt3 = null;
    private JLabel lblMsg = null;
    private TDSLanguageUtils mess = null;
    private boolean flag = false;
    private GestorIncidenciesInterface objServeis = null;

    /**
     * This is the default constructor
     */
    public Client() {
        super();
        messagesFile();
        initialize(1);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Client thisClass = new Client();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize(int accio) {
        int _accio = accio;
        this.setSize(700, 425);
        this.setJMenuBar(getMenu());
        switch (_accio) {

            // Finestra inicial --> conexion con el servidor

            case 1:
                this.setContentPane(getJContentPane());
                break;
            case 2:
                PantallaAltaNova Pnt;
                try {
                    Pnt = new PantallaAltaNova(objServeis);
                    this.setContentPane(Pnt);
                } catch (RemoteException e) {
                    JOptionPane.showMessageDialog(null,
                            mess.getMessage("client.err.rmi") + "\n" + e.getMessage(),
                            mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                }
                break;

            case 3:
                PantallaActuacions Pnt2;
                try {
                    Pnt2 = new PantallaActuacions(objServeis);
                    this.setContentPane(Pnt2);
                } catch (RemoteException e) {
                    JOptionPane.showMessageDialog(null,
                            mess.getMessage("client.err.rmi") + "\n" + e.getMessage(),
                            mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                }
                break;

            case 4:
                PantallaHistoric Pnt3;
                try {
                    Pnt3 = new PantallaHistoric(objServeis);
                    this.setContentPane(Pnt3);
                } catch (RemoteException e) {
                    JOptionPane.showMessageDialog(null,
                            mess.getMessage("client.err.rmi") + "\n" + e.getMessage(),
                            mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                }
                break;
        }
        this.setTitle(mess.getMessage("client.title"));
        setShow();
    }

    /**
     * Comprova l’existència de l'arxiu de missatges
     * 
     * @return void
     */
    private void messagesFile() {
        //           
        boolean exist = mess.setDefaultLanguage("i18n/messages");
        if (!exist) {
            System.out.println("No es possible iniciar l'aplicació, no existeix l'arxiu de misatges 'i18n/messages*.properties'");
            System.exit(-1);
        }
    }

    /**
     * Bloqueja opcions si el client no esta connectat 
     * si esta connectat desbloqueja les opcions
     * 
     * @return void
     */
    private void setShow() {
        if (!flag) {
            mnuIniciIt1.setEnabled(true);
            mnuIncidencies.setEnabled(false);
        } else {
            mnuIniciIt1.setEnabled(false);
            mnuIncidencies.setEnabled(true);
        }
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            lblMsg = new JLabel();
            lblMsg.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            lblMsg.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            lblMsg.setFont(new Font("Arial", Font.BOLD, 20));
            lblMsg.setText(mess.getMessage("client.texte"));
            lblMsg.setBounds(20, 150, 630, 24);
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(lblMsg, null);
        }
        return jContentPane;
    }

    /**
     * This method initializes menu	
     * 	
     * @return javax.swing.JMenuBar	
     */
    private JMenuBar getMenu() {
        if (mnuMain == null) {
            mnuMain = new JMenuBar();
            mnuMain.add(getMenuInici());
            mnuMain.add(getMenuIncidencies());
        }
        return mnuMain;
    }

    /**
     * This method initializes menuInici	
     * 	
     * @return javax.swing.JMenu	
     */
    private JMenu getMenuInici() {
        if (mnuInici == null) {
            mnuInici = new JMenu();
            mnuInici.setText(mess.getMessage("client.menu.inici"));
            mnuInici.add(getMnuIniciIt1());
            mnuInici.add(getMnuIniciIt2());
        }
        return mnuInici;
    }

    /**
     * This method initializes menuTransport	
     * 	
     * @return javax.swing.JMenu	
     */
    private JMenu getMenuIncidencies() {
        if (mnuIncidencies == null) {
            mnuIncidencies = new JMenu();
            mnuIncidencies.setText(mess.getMessage("client.menu.incidencies"));
            mnuIncidencies.add(getMnuIncidIt1());
            mnuIncidencies.add(getMnuIncidIt2());
            mnuIncidencies.add(getMnuIncidIt3());
        }
        return mnuIncidencies;
    }

    /**
     * This method initializes menuInici1	
     * 
     * Permitela conexion con el servidor RMI	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getMnuIniciIt1() {
        if (mnuIniciIt1 == null) {
            mnuIniciIt1 = new JMenuItem();
            mnuIniciIt1.setText(mess.getMessage("client.menu.inici.item1"));
            mnuIniciIt1.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    try {
                        if (objServeis == null) {
                            try {
                                objServeis = (GestorIncidenciesInterface) Naming.lookup("//localhost/Services");
                                flag = true;
                                setShow();
                            } catch (RemoteException e1) {
                                objServeis = null;
                                JOptionPane.showMessageDialog(null,
                                        mess.getMessage("client.err.conn") + "\n" + e1.getMessage(),
                                        mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                            } catch (NotBoundException e2) {
                                objServeis = null;
                                JOptionPane.showMessageDialog(null,
                                        mess.getMessage("client.err.conn") + "\n" + e2.getMessage(),
                                        mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                            } catch (Exception e3) {
                                objServeis = null;
                                JOptionPane.showMessageDialog(null,
                                        mess.getMessage("client.err.conn") + "\n" + e3.getMessage(),
                                        mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    } catch (Exception eAl) {
                        JOptionPane.showMessageDialog(null,
                                mess.getMessage("client.err.conn") + "\n" + eAl.getMessage(),
                                mess.getMessage("client.err"), JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
        }
        return mnuIniciIt1;
    }

    /**
     * This method initializes menuInici2	
     * 
     * Salir del programa
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getMnuIniciIt2() {
        if (mnuIniciIt2 == null) {
            mnuIniciIt2 = new JMenuItem();
            mnuIniciIt2.setText(mess.getMessage("client.menu.inici.item2"));
            mnuIniciIt2.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    System.exit(0);
                }
            });
        }
        return mnuIniciIt2;
    }

    /**
     * This method initializes menuTransport1	
     *
     * Estado actual de las votaciones 	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getMnuIncidIt1() {
        if (mnuIncidenciesIt1 == null) {
            mnuIncidenciesIt1 = new JMenuItem();
            mnuIncidenciesIt1.setText(mess.getMessage("client.menu.incidencies.item1"));
            mnuIncidenciesIt1.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    initialize(2);
                }
            });
        }
        return mnuIncidenciesIt1;
    }

    /**
     * This method initializes menuTransport2	
     * 	
     * Ralizar votacion
     * 
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getMnuIncidIt2() {
        if (mnuIncidenciesIt2 == null) {
            mnuIncidenciesIt2 = new JMenuItem();
            mnuIncidenciesIt2.setText(mess.getMessage("client.menu.incidencies.item2"));
            mnuIncidenciesIt2.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    initialize(3);
                }
            });
        }
        return mnuIncidenciesIt2;
    }

    /**
     * This method initializes menuTransport3	
     * 	
     * Resultados mejor equipo del a�os
     * 
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getMnuIncidIt3() {
        if (mnuIncidenciesIt3 == null) {
            mnuIncidenciesIt3 = new JMenuItem();
            mnuIncidenciesIt3.setText(mess.getMessage("client.menu.incidencies.item3"));
            mnuIncidenciesIt3.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    initialize(4);
                }
            });
        }
        return mnuIncidenciesIt3;
    }
}
